﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;

namespace LCA.Community.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for HospitalsRepositoryTest and is intended
    ///to contain all HospitalsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class HospitalsRepositoryTest : BaseTestClass
    {
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        private List<HospitalPatient> _hospitalPatients;
        private HospitalsFactory _hospitalsfactory = new HospitalsFactory();
        private HospitalPatientFactory _hospitalPatientFactory = new HospitalPatientFactory();
        private List<Hospital> _hospitals;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(5);
            _users.ForEach(user => usersRepository.Add(user));

            var hospitalsRepository = _unityContainer.Resolve<IHospitalsRepository>();
            _hospitals = _hospitalsfactory.CreateList(2);
            _hospitals.ForEach(h => hospitalsRepository.Add(h));
            _hospitalPatients = new List<HospitalPatient>();

            var patientsRepository = _unityContainer.Resolve<IHospitalPatientsRepository>();
            var patient = _hospitalPatientFactory.CreateEntity(_users[0].RowKey, _hospitals[0].RowKey);
            patientsRepository.Add(patient);
            _hospitalPatients.Add(patient);

            patient = _hospitalPatientFactory.CreateEntity(_users[1].RowKey, _hospitals[0].RowKey);
            patientsRepository.Add(patient);
            _hospitalPatients.Add(patient);

            patient = _hospitalPatientFactory.CreateEntity(_users[2].RowKey, _hospitals[1].RowKey);
            patientsRepository.Add(patient);
            _hospitalPatients.Add(patient);
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var hospitalsRepository = _unityContainer.Resolve<IHospitalsRepository>();
            var patientsRepository = _unityContainer.Resolve<IHospitalPatientsRepository>();
            usersRepository.DeleteTestEntities();
            hospitalsRepository.DeleteTestEntities();
            patientsRepository.DeleteTestEntities();

        }
        
        /// <summary>
        ///A test for GetHospitalOfUser
        ///</summary>
        [TestMethod()]
        public void GetHospitalOfUserTest()
        {
            var target = _unityContainer.Resolve<IHospitalsRepository>();
            string userID = _users[0].RowKey;
            Hospital expected = _hospitals[0];

            Hospital actual;
            actual = target.GetHospitalOfUser(userID);
            Assert.AreEqual(expected, actual);
        }
    }
}
